<template>
  <div style="text-align: center; font-size: 28px">{{ dataForm.title }}</div>
  <el-divider></el-divider>
  <div v-html="dataForm.content"></div>
  <div>
    <hr size="1" color="#ddd" style="margin: 30px 0 10px 0" />
    <span>
      <el-icon style="color: #e6444a"><user-solid /></el-icon> {{ $t("notice.senderName") }}：{{ dataForm.senderName }}
    </span>
    <el-divider direction="vertical"></el-divider>
    <span>
      <el-icon style="color: #e6444a"><time /></el-icon> {{ $t("notice.senderDate") }}：{{ dataForm.senderDate }}
    </span>
    <el-divider direction="vertical" style="margin: 0px; padding: 0px"></el-divider>
    <span>
      <el-icon style="color: #e6a23c"><s-order /></el-icon>
      {{ $t("notice.type") }}：
      {{ state.getDictLabel("notice_type", dataForm.noticeType) }}
    </span>
    <hr size="1" color="#ddd" style="margin: 10px 0 30px 0" />
  </div>
  <div style="text-align: center">
    <el-button type="info" @click="addPart" v-show="state.partButton">加入</el-button>
    <el-button type="danger" @click="state.closeCurrentTab();onRefresh()">{{ $t("notice.close") }}</el-button>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { onMounted, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import { useRoute } from "vue-router";
import {ElMessage} from "element-plus";
import {useAppStore} from "@/store";
import emits from "@/utils/emits";
import {EMitt} from "@/constants/enum";
const route = useRoute();

const dataForm = reactive({
  id: "",
  noticeType: 0,
  senderDate: "",
  senderName: "",
  content: "",
  title: ""
});

const view = reactive({
  getDataListURL: "/sys/notice/user/page",
  createdIsNeed: false,
  activatedIsNeed: false,
  getDataListIsPage: true,
  partButton:false,
});

const state = reactive({ ...useView(view), ...toRefs(view) });

onMounted(() => {
  dataForm.id = route.query.id as string;
  dataForm.content = "";
  dataForm.senderDate = "";
  dataForm.senderName = "";
  dataForm.title = "";
  getInfo();
  // state.query();
});
const onRefresh = () => {
  emits.emit(EMitt.OnReloadTabPage);
};
// 获取信息
const getInfo = () => {
  baseService.get(`/sys/notice/${dataForm.id}`).then((res) => {
    console.log(res)
    Object.assign(dataForm, res.data);
    if(dataForm.noticeType===3) {
      state.partButton=true
    }
    console.log(dataForm.noticeType)
    console.log(state.partButton)
  });

};
const dataPartForm = reactive({
  id: "" as string,
  participantId: "",
  projectId: "" as any,
  role: 1,
  content:"",
  createor:"",
  createData:"" as any,
});
const store = useAppStore()
const addPart = ()=>{
  dataPartForm.participantId=store.state.user.id
  dataPartForm.content=dataForm.content
  baseService.get("/poap/poapparticipantproject/isParticipant",{participantId:dataPartForm.participantId,content:dataPartForm.content}).then(res=>{
    console.log(res)
    if(res.code!=0) return false
    if(res.data) ElMessage.info("已经加入该项目")
    else {
      baseService.post("/poap/poapparticipantproject", dataPartForm).then((res) => {
        console.log(res)
        if(res.code!=0) return false
        dataPartForm.content=res.data.content
        dataPartForm.participantId=res.data.participantId
        dataPartForm.projectId=res.data.projectId
        dataPartForm.role=res.data.role
        ElMessage.success({
          message: "加入成功",
          duration: 500,
          onClose: () => {
            state.closeCurrentTab()
          }
        });
      })
    }
  })

}
</script>
